<template>
  <div>
    <!-- 搜索条件 -->
    <div id="foldDiv">
      <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
        <div>
          <span class="form-item-label">类型</span>
          <el-select v-model="queryParams.type" style="width:105px" size="mini" @change="getList" clearable>
            <el-option v-for="dict in dict.type.stock_up_statistics_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
          <span class="form-item-label" style="margin-left:10px">收价</span>
          <el-select v-model="queryParams.closeAverageLineType" style="width:65px" size="mini" @change="getList" clearable placeholder="请选择">
            <el-option v-for="item in closeAverageLineTypeOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <span class="form-item-label" style="margin-left: 10px">均类</span>
          <el-select v-model="queryParams.averageLineType" style="width:95px" size="mini" @change="getList" clearable placeholder="请选择">
            <el-option v-for="item in averageLineTypeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
          <span class="form-item-label" style="margin-left: 10px">价格</span>
          <el-select v-model="queryParams.closePriceRange" style="width:95px" size="mini" @change="changeClosePriceRange" clearable placeholder="请选择">
            <el-option v-for="item in closePriceRangeOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
          <el-button style="margin-left: 10px" type="danger" icon="el-icon-s-data" size="mini" @click="handleGenerateKLine">EchartsK</el-button>
          <el-button type="danger" icon="el-icon-s-data" size="mini" @click="handleGenerateMyKLine">MyK</el-button>
          <el-button icon="el-icon-refresh" style="color: #FFFFFF;background-color: #d521d0" size="mini" @click="resetQuery">重置</el-button>
          <el-button icon="el-icon-back" type="danger" style="margin-left: 5px" size="mini" @click="back">back</el-button>
        </div>
        <div>
          <span class="form-item-label">上涨</span>
          <el-input id="input1" style="width:105px" v-model="queryParams.riseDays" size="mini"/>
          <span class="form-item-label" style="margin-left: 10px">连涨</span>
          <el-input v-model="queryParams.continueRiseDays" style="width:65px" size="mini"/>
          <span class="form-item-label" style="margin-left: 10px">涨停</span>
          <el-input v-model="queryParams.riseStopDays" style="width:95px" size="mini"/>
          <span class="form-item-label" style="margin-left: 10px">连涨停</span>
          <el-input v-model="queryParams.continueRiseStopDays" style="width:50px" size="mini"/>
          <span class="form-item-label" style="margin-left: 10px">连跌</span>
          <el-input v-model="queryParams.continueFallDays" style="width:60px;margin-right: 10px" size="mini" @keyup.enter.native="handleQuery"/>
        </div>
        <div>
          <span class="form-item-label">MACD金叉</span>
          <el-select v-model="queryParams.inMacdGoldFork" size="mini" style="width:65px;" placeholder="请" @change="getList" clearable>
            <el-option v-for="dict in dict.type.stock_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
          <span class="form-item-label" style="margin-left:10px">零上</span>
          <el-select v-model="queryParams.macdGreaterThan0" size="mini" style="width:65px;" placeholder="请" @change="getList" clearable>
            <el-option v-for="dict in dict.type.stock_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
          <span class="form-item-label" style="margin-left:10px">KDJ金叉</span>
          <el-select v-model="queryParams.inKdjGoldFork" size="mini" style="width:65px" placeholder="请" @change="getList" clearable>
            <el-option v-for="dict in dict.type.stock_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
          <span class="form-item-label" style="margin-left:10px">超跌</span>
          <el-select v-model="queryParams.kdjOverfall" size="mini" style="width:65px;" placeholder="请" @change="getList" clearable>
            <el-option v-for="dict in dict.type.stock_yes_no" :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
          <span class="form-item-label" style="margin-left:10px">倍量</span>
          <el-input style="width:60px" v-model="queryParams.volumeTimes" size="mini" clearable/>
        </div>
      </el-form>
    </div>

    <!--K线图-->
    <div v-if="isShowKLine">
      <template v-for="(item, index) in stockCodeKLine">
        <el-card shadow="always" style="margin-top:20px">
          <div :id="'d'+item.split('_')[0]" style="width: 100%;height: 600px"></div>
        </el-card>
      </template>
    </div>

    <!--我的K线图-->
    <div v-if="isShowMyKLine">
      <template v-for="(item) in stockCodeKLine">
        <el-card style="margin-top:20px">
          <div :id="'d'+item.split('_')[0]" style="width: 100%;height: 600px"></div>
        </el-card>
      </template>
    </div>

    <pl-table v-loading="loading" border @sort-change="handleSortChange" ref="plTable" :height="tableHeight" :data="stockUpStatisticsList" selectTrClass="selectTr" header-drag-style :dataChangesScrollTop="false" @selection-change="handleSelectionChange" fixedColumnsRoll inverse-current-row bigDataCheckbox use-virtual>
      <pl-table-column type="selection" width="50"/>
      <pl-table-column label="编码" width="70" prop="stockCode"/>
      <pl-table-column label="股票名称" width="80" prop="stockName"/>
      <pl-table-column label="价格" width="65" prop="closePrice"/>
      <pl-table-column label="统计" width="50" prop="statisticsDays"/>
      <pl-table-column label="类型" width="80" sortable="custom" prop="type">
        <template v-slot="scope">
          <dict-tag :options="dict.type.stock_up_statistics_type" :value="scope.row.type"/>
        </template>
      </pl-table-column>
      <pl-table-column label="连涨" width="70" sortable="custom" prop="continueRiseDays"/>
      <pl-table-column label="连涨幅度" sortable="custom" prop="continueRiseRate"/>
      <pl-table-column label="上涨" width="70" sortable="custom" prop="riseDays"/>
      <pl-table-column label="涨停" width="70" sortable="custom" prop="riseStopDays"/>
      <pl-table-column label="下跌" width="70" sortable="custom" prop="fallDays"/>
      <pl-table-column label="跌停" width="70" sortable="custom" prop="fallStopDays"/>
      <pl-table-column label="连涨停" width="75" sortable="custom" prop="continueRiseStopDays"/>
      <pl-table-column label="连跌" sortable="custom" prop="continueFallDays"/>
      <pl-table-column label="连跌幅度" sortable="custom" prop="continueFallRate"/>
      <pl-table-column label="连跌停" sortable="custom" prop="continueFallStopDays"/>
      <pl-table-column label="K线名称" prop="kLineName"/>
    </pl-table>

    <!-- 分页-->
    <pagination @pagination="getList" v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"/>

    <!-- 滑动到页面底部会看见-->
    <el-backtop>返回</el-backtop>

  </div>
</template>

<script>
import {listStockUpStatistics, averageLineType} from '@/api/eye/stockUpStatistics'
import {renderKLine} from '@/api/eye/kline'
import {getHistoryPriceByStockCode} from '@/api/eye/stockPrice'
import Vue from 'vue'
import ElementResizeDetectorMaker from 'element-resize-detector'
import {initDragonKLine} from "@/api/eye/myKLine";

import {PlTable, PlTableColumn} from 'pl-table';

Vue.prototype.$erd = ElementResizeDetectorMaker()

export default {
  name: 'StatisticsB',
  dicts: ['stock_up_statistics_type', 'stock_yes_no'],
  components: {
    PlTable,
    PlTableColumn
  },
  data() {
    return {
      // 均线类型
      averageLineTypeOptions: [{value: 1, label: '均线向上'}, {value: 2, label: 'MA5>10'}, {value: 3, label: 'MA5>20'}, {value: 4, label: 'MA5>30'}, {value: 5, label: 'MA5>60'}, {value: 6, label: 'MA5>125'}, {value: 7, label: 'MA5>250'}],
      // 收盘价站上均线价格
      closeAverageLineTypeOptions: [{value: 1, label: '站上5'}, {value: 2, label: '勇攀10'}, {value: 3, label: '月线20'}, {value: 4, label: '生命30'}, {value: 5, label: '中期60'}, {value: 6, label: '半年125'}, {value: 7, label: '年线250'}],
      // 价格
      closePriceRangeOptions: [{value: '0-3', label: '0-3'}, {value: '3-5', label: '3-5'}, {value: '5-10', label: '5-10'}, {value: '10-20', label: '10-20'}, {value: '20-50', label: '20-50'}, {value: '0-50', label: '0-50'},{value: '50-100', label: '50-100'}, {value: '100-5000', label: '100-'}],
      tableHeight: 200,
      // 展示我的K线
      isShowMyKLine: false,
      // K线图是否显示
      isShowKLine: true,
      // 遮罩层
      loading: true,
      // 选中数组
      selectedValues: [],
      stockCodeKLine: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 股票指标统计信息表格数据
      stockUpStatisticsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 100,
        stockCode: null,
        stockName: null,
        type: null,
        statisticsDays: null,
        riseDays: null,
        fallDays: null,
        continueRiseDays: null,
        continueRiseRate: null,
        continueFallDays: null,
        continueFallRate: null,
        riseStopDays: null,
        fallStopDays: null,
        continueRiseStopDays: null,
        continueFallStopDays: null,
        kLineName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  watch: {
    // 监测输入框的值，并执行查询操作
    "queryParams.riseDays": function () {
      this.getList()
    }, "queryParams.riseStopDays": function () {
      this.getList()
    },
    "queryParams.continueRiseDays": function () {
      this.getList()
    },
    "queryParams.volumeTimes": function () {
      this.getList()
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getTableMaxHeight()
    let _this = this
    window.onresize = function () {
      console.log('onresize 统计')
      //页面改变重新自适应高度
      _this.getTableMaxHeight()
    }
    this.$erd.listenTo(document.getElementById('foldDiv'), () => {
      console.log('listen 统计')
      _this.getTableMaxHeight()
    })
  },
  methods: {
    //获取容器当前高度，重设表格的最大高度
    getTableMaxHeight() {
      this.$nextTick(function () {
        let t = document.querySelector('#tableDiv')
        console.log('统计 重置高度')
        this.tableHeight = window.innerHeight - t.getBoundingClientRect().top - 320
      })
    },
    changeClosePriceRange(v){
      if(v){
        console.log('you')
        console.log(v)
        this.queryParams.closePriceStart = v.split('-')[0]
        this.queryParams.closePriceEnd = v.split('-')[1]
      }else {
        this.queryParams.closePriceStart = null
        this.queryParams.closePriceEnd = null
      }
      this.getList()
    },
    /** 查询股票指标统计信息列表 */
    getList() {
      this.loading = true
      listStockUpStatistics(this.queryParams).then(response => {
        this.stockUpStatisticsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 排序
    handleSortChange(column) {
      // 排序字段
      let sortColumn = column.prop
      // 升序还是降序
      let sortOrder = column.order
      this.queryParams.orderByColumn = sortColumn
      if (sortOrder === 'ascending') {
        console.log('升序')
        this.queryParams.isAsc = 'asc'
      } else {
        console.log('降序')
        this.queryParams.isAsc = 'desc'
      }
      this.getList()
    },
    handleAverageLineType(n) {
      console.log(n)
      this.loading = true
      this.queryParams.pageSize = 5000
      this.queryParams.averageLineType = n

      averageLineType(this.queryParams).then(response => {
        this.stockUpStatisticsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockCode: null,
        stockName: null,
        statisticsDays: null,
        riseDays: null,
        fallDays: null,
        continueRiseDays: null,
        continueRiseRate: null,
        continueFallDays: null,
        continueFallRate: null,
        riseStopDays: null,
        fallStopDays: null,
        continueRiseStopDays: null,
        continueFallStopDays: null,
        createTime: null,
        updateTime: null,
        kLineName: null
      }
      console.log(this.queryParams)
      this.queryParams = {
        riseDays: null
      }
      console.log(this.queryParams)
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.isShowKLine = false
      this.isShowMyKLine = false
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.reset()
      this.handleQuery()
    },
    /** 返回 */
    back() {
      this.isShowKLine = false
      this.isShowMyKLine = false
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection)
      this.selectedValues = selection.map(item => item.stockCode + '_' + item.stockName)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 生成K线图 */
    handleGenerateKLine() {
      this.stockCodeKLine = this.selectedValues
      this.isShowKLine = true
      this.selectedValues.forEach(item => {
        let code = item.split('_')[0]
        getHistoryPriceByStockCode(code).then(response => {
          renderKLine('d' + code, item, response.data, 1)
        })
      })
    },
    /** 生成我的K线图 */
    handleGenerateMyKLine() {
      this.stockCodeKLine = this.selectedValues
      this.isShowMyKLine = true
      this.stockCodeKLine.forEach(item => {
        let code = item.split('_')[0]
        getHistoryPriceByStockCode(code).then(response => {
          initDragonKLine('d' + code, item, response.data.date, response.data.price)
        })
      })
    }
  }
}
</script>

<style>
.form-item-label {
  text-align: right;
  vertical-align: middle;
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  font-weight: 700;
}
</style>

